﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.XtraEditors;

namespace CharacterTrailClient.Controls
{
    public partial class BarCodeAndName : XtraUserControl
    {
        private readonly IStaticDataQuerier _staticDataQuerier;
        private bool _isParentProduct = true;

        private TextBoxInputBarCodeValidatedEndEventHandler _textBoxInputBarCodeValidatedEnd;

        public BarCodeAndName()
        {
            _staticDataQuerier = InstanceFactory.GetStaticDataQuerier();
            InitializeComponent();
            textEditBarCode.KeyPress += TextEditBarCodeKeyPress;
            InputBarCodeIsOk = false;
        }

        public Color LabelColor
        {
            get { return labelControlLabel.ForeColor; }
            set { labelControlLabel.ForeColor = value; }
        }

        public bool InputBarCodeIsOk { get; private set; }

        public int Index { get; set; }

        public TProductHeaderModel FProductHeaderModel { get; set; }

        public TProductDetailModel FProductDetailModel { get; set; }

        //public TBomHeaderModel FBomHeaderModel { get; set; }

        //public TBomMainModel FBomMainModel { get; set; }

        //public TBomEntryModel FBomEntryModel { get; set; }

        public lmbcprintlogModel FlmbcprintlogModel { get; set; }

        /// <summary>
        /// 是否是最后一个控件
        /// </summary>
        public bool IsTheLastControl { get; set; }

        /// <summary>
        ///   是否是产品,true产品,false物料
        /// </summary>
        [ToolboxItem("BarCodeAndName")]
        public bool IsParentProduct
        {
            get { return _isParentProduct; }
            set { _isParentProduct = value; }
        }

        public string BarCode
        {
            get { return textEditBarCode.Text; }
            set { textEditBarCode.Text = value; }
        }

        public string FName
        {
            get { return textEditName.Text; }
            set { textEditName.Text = value; }
        }

        [ToolboxItem("BarCodeAndName")]
        public string LabelText
        {
            get { return labelControlLabel.Text; }
            set { labelControlLabel.Text = value; }
        }

        #region deleted

        //private void TextEditBarCodeEditValueChanged(object sender, EventArgs e)
        //{
        //    this.labelControlLabel.ForeColor = Color.Red;
        //    InputBarCodeIsOk = false;
        //    if (!String.IsNullOrEmpty(this.textEditBarCode.Text.Trim()))
        //    {
        //        //查询条码表，必须在条码表中找到记录才行
        //        var imbcprintlog = _staticDataQuerier.GetlmbcprintlogModel(this.textEditBarCode.Text.Trim());
        //        if(imbcprintlog == null)
        //        {
        //            this.textEditBarCode.Text = "";
        //            FProductHeaderModel = null;
        //            FProductDetailModel = null;
        //            FlmbcprintlogModel = null;
        //            textEditBarCode.Focus();
        //        }
        //        else
        //        {
        //            FlmbcprintlogModel = imbcprintlog;
        //            if (this.IsParentProduct)
        //            {
        //                FProductHeaderModel = new TProductHeaderModel
        //                {
        //                    ProductItemId = FlmbcprintlogModel.Fitemid,
        //                    ProductItemBarCode = FlmbcprintlogModel.Fbarcode,
        //                    EditFlag = Enums.EditFlag.New,
        //                    InputId = UserSession.CurrentUser.UserId,
        //                    InputDate = DateTime.Now
        //                };
        //                this.labelControlLabel.ForeColor = Color.Black;
        //            }
        //            else
        //            {
        //                if (imbcprintlog.Fitemid != FBomHeaderModel.ChildItemId)
        //                {
        //                    this.textEditBarCode.Text = "";
        //                    textEditBarCode.Focus();
        //                }
        //                else
        //                {
        //                    //产生一个对象
        //                    FProductDetailModel = new TProductDetailModel
        //                                              {
        //                                                  ChildItemId = FlmbcprintlogModel.Fitemid,
        //                                                  ChildItemBarCode = imbcprintlog.Fbarcode,
        //                                                  Idx = this.Index,
        //                                                  RowGuid = Guid.NewGuid().ToString("N"),
        //                                                  EditFlag = Enums.EditFlag.New,
        //                                                  InputId = UserSession.CurrentUser.UserId,
        //                                                  InputDate = DateTime.Now
        //                                              };
        //                    this.labelControlLabel.ForeColor = Color.Black;
        //                    InputBarCodeIsOk = true;
        //                }
        //            }
        //        }

        //        //激活委托对象
        //        if (_textBoxInputBarCodeValidatedEnd != null)
        //        {
        //            _textBoxInputBarCodeValidatedEnd(this);
        //        }
        //    }
        //    else
        //    {
        //        FProductHeaderModel = null;
        //        FProductDetailModel = null;
        //        FlmbcprintlogModel = null;
        //    }
        //}

        #endregion

        /// <summary>
        ///   条码框输入符合要求的条码后，通过数据库校验，存在该条码，激活该事件。
        /// </summary>
        public event TextBoxInputBarCodeValidatedEndEventHandler TextBoxInputBarCodeValidatedEnd
        {
            add
            {
                _textBoxInputBarCodeValidatedEnd += value;
            }
            remove 
            {
                _textBoxInputBarCodeValidatedEnd -= value; 
            }
        }

        public void ResetBarCode()
        {
            textEditBarCode.EditValue = string.Empty;
            textEditName.EditValue = string.Empty;
        }

        private void TextEditBarCodeKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char) Keys.Enter 
                && !String.IsNullOrEmpty(textEditBarCode.Text.Trim()))
            {
                //this.labelControlLabel.ForeColor = Color.Red;
                InputBarCodeIsOk = false;
                if (!String.IsNullOrEmpty(textEditBarCode.Text.Trim()))
                {
                    //查询条码表，必须在条码表中找到记录才行
                    var printlogModel = _staticDataQuerier.GetlmbcprintlogModel(textEditBarCode.Text.Trim());
                    if (printlogModel == null)
                    {
                        ResetBarCode();
                        FProductHeaderModel = null;
                        FProductDetailModel = null;
                        FlmbcprintlogModel = null;
                        textEditBarCode.Focus();
                    }
                    else
                    {
                        //FlmbcprintlogModel = imbcprintlog;
                        if (IsParentProduct)
                        {
                            //FProductHeaderModel = new TProductHeaderModel
                            //                          {
                            //                              ProductItemId = FlmbcprintlogModel.Fitemid,
                            //                              ProductItemBarCode = FlmbcprintlogModel.Fbarcode,
                            //                              EditFlag = Enums.EditFlag.New,
                            //                              InputId = UserSession.CurrentUser.UserId,
                            //                              InputDate = DateTime.Now
                            //                          };
                            //this.labelControlLabel.ForeColor = Color.Black;
                        }
                        else
                        {
                            #region 暂时作废

                            ////如果是物料的话，首先看是否该条码已经存在wraplist中，如果存在，
                            ////则清空，焦点继续留在该控件，如果不存在，则绑定
                            //var list = this.ParentWrapList.FindAll(
                            //    wrap => wrap.BarCodeAndNameObject.FlmbcprintlogModel != null &&
                            //        wrap.BarCodeAndNameObject.FlmbcprintlogModel.Fbarcode == imbcprintlog.Fbarcode);

                            //if (list.Count > 0)
                            //{
                            //    //已经存在
                            //    this.textEditBarCode.Text = "";
                            //    textEditBarCode.Focus();
                            //}
                            //else
                            //{
                            //    var list2 = this.ParentWrapList.FindAll(
                            //        wrap =>
                            //            wrap.BarCodeAndNameObject.FlmbcprintlogModel == null &&
                            //            wrap.BarCodeAndNameObject.FProductDetailModel == null &&
                            //            wrap.BarCodeAndNameObject.FBomEntryModel.MaterialItemId == imbcprintlog.Fitemid);

                            //    //如果存在，则找到进行绑定
                            //    if (list2.Count > 0)
                            //    {

                            //        FlmbcprintlogModel = imbcprintlog;
                            //        //产生一个对象
                            //        FProductDetailModel = new TProductDetailModel
                            //                                  {
                            //                                      ChildItemId = FlmbcprintlogModel.Fitemid,
                            //                                      ChildItemBarCode = imbcprintlog.Fbarcode,
                            //                                      Idx = this.Index,
                            //                                      RowGuid = Guid.NewGuid().ToString("N"),
                            //                                      EditFlag = Enums.EditFlag.New,
                            //                                      InputId = UserSession.CurrentUser.UserId,
                            //                                      InputDate = DateTime.Now
                            //                                  };
                            //        this.labelControlLabel.ForeColor = Color.Black;
                            //        InputBarCodeIsOk = true;
                            //    }
                            //}

                            ////if (imbcprintlog.Fitemid != FBomHeaderModel.ChildItemId)
                            ////{
                            ////    this.textEditBarCode.Text = "";
                            ////    textEditBarCode.Focus();
                            ////}
                            ////else
                            ////{
                            ////    //产生一个对象
                            ////    FProductDetailModel = new TProductDetailModel
                            ////                              {
                            ////                                  ChildItemId = FlmbcprintlogModel.Fitemid,
                            ////                                  ChildItemBarCode = imbcprintlog.Fbarcode,
                            ////                                  Idx = this.Index,
                            ////                                  RowGuid = Guid.NewGuid().ToString("N"),
                            ////                                  EditFlag = Enums.EditFlag.New,
                            ////                                  InputId = UserSession.CurrentUser.UserId,
                            ////                                  InputDate = DateTime.Now
                            ////                              };
                            ////    this.labelControlLabel.ForeColor = Color.Black;
                            ////    InputBarCodeIsOk = true;
                            ////}

                            #endregion
                        }
                        //激活委托对象
                        if (_textBoxInputBarCodeValidatedEnd != null)
                        {
                            _textBoxInputBarCodeValidatedEnd(this, printlogModel);
                        }
                    }
                }
                else
                {
                    FProductHeaderModel = null;
                    FProductDetailModel = null;
                    FlmbcprintlogModel = null;
                }
            }
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            textEditBarCode.Focus();
        }
    }
}